﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Com.CMS263.Util;
using System.Net;
using System.IO;
namespace Com.CMS263.Util
{
    class SecurityHandle : IHttpModule
    {

        #region IHttpModule 成员

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        }

        void context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;

            string url = context.Request.Path;
            string ext = url.Split('.')[url.Split('.').Length - 1];

            if (ext.ToLower() == "aspx"||ext.ToLower()=="asp"||ext.ToLower()=="ashx"||ext.ToLower()=="txt")
            {

                if (url.ToLower().IndexOf("/manage/") >= 0 && url.ToLower() != "/manage/userlogin.aspx")
                {

                    if (!AdminLoginCookie.IsUserLogin())
                    {
                        context.Server.Transfer("/manage/userLogin.aspx");
                    }
                    else
                    {
                        AdminLoginInfo li = AdminLoginCookie.GetLoginInfo();
                        string[] str = li.Power.Split(',');
                        for (int i = 0; i < str.Length - 1; i++)
                        {
                            int menuId = Convert.ToInt32(context.Request.QueryString.Get("menuId"));
                            if (menuId == Convert.ToInt32(str[i]))
                            {
                                StringUtil.Alert("对不起,您的权限不够", "-1");
                            }

                        }

                    }

                }
                if (url.ToLower().IndexOf("/work/") >= 0)
                {
                    if (!LoginCookie.IsUserLogin())
                    {
                        StringUtil.Alert("尚未登录或登录超时,请您登录", "/Login.aspx");
                    }
                }
                if (url.ToLower().IndexOf("/busermanage/") >= 0)
                {
                    if (!BusinessLoginCookie.IsBusinessUserLogin())
                    {
                        StringUtil.Alert("尚未登录或登录超时,请您登录", "/BuserLogin.aspx");
                    }
                }
                if (url.ToLower().IndexOf("/bin/") >= 0)
                {
                    context.Response.Redirect("/index.aspx");
                }
                if (url.ToLower().IndexOf("/kefu/") >= 0)
                {
                    if (!KeFuLoginCookie.IsUserLogin())
                    {
                        StringUtil.Alert("尚未登录或登录超时,请您登录", "/KefuLogin.aspx");
                    }
                }
            }          
        }
        void context_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpContext ctx = ((HttpApplication)sender).Context;
           
        }
        #endregion
    }
}
